package training03;

import java.util.Arrays;

public class SuperArray<T> {
    private Object [] objs = new Object[0];
    public T get(int index){
        return (T)objs[index];
    }
    /**
     * 增加元素
     * @param obj
     */
    public void add(T obj){
        Object [] tempAry = new Object[objs.length + 1];
        for(int i = 0;i < objs.length;i++){
            tempAry[i] = objs[i];

        }
        tempAry[tempAry.length - 1] = obj;
        objs = tempAry;
    }

    /**
     * 删除元素
     * @param index 指定要删除元素序号
     */
    public void remove(int index){
        Object [] tempAry = new Object[objs.length - 1];
        for(int i = 0;i < tempAry.length;i++){
            if(i < index){
                tempAry[i] = objs[i];
            }else{
                tempAry[i] = objs[i+1];
            }
        }
        objs = tempAry;

    }

    /**
     * 获取数组的长度
     * @return
     */
    public int size(){
        return objs.length;
    }

    @Override
    public String toString() {
        return Arrays.toString(objs);
    }
}
